package dao;

import dao.impl.RsDaoImpl;
import entity.Rs;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RsDao extends BaseDao implements RsDaoImpl {
    Rs rs1 = null;
    @Override
    public List<Rs> xianshi() {
        List<Rs> list = new ArrayList<>();
        super.init();
        conn =super.getConnection();
        try {
            pstmt = conn.prepareStatement("select * from ry");
            rs =pstmt.executeQuery();
            while (rs.next()){
                rs1 = new Rs();
                rs1.setPost_code(rs.getString("post_code"));
                rs1.setPost_id(rs.getString("post_id"));
                rs1.setPost_name(rs.getString("psot_name"));
                rs1.setStatus(rs.getString("status"));
                rs1.setPost_sort(rs.getString("post_sort"));
                rs1.setTime(rs.getString("time"));
                list.add(rs1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int add(String code,String name,String sort,String status,String time) {
        int add = 0;
        super.init();
        conn = super.getConnection();
        try {
            pstmt = conn.prepareStatement("INSERT INTO `microblog`.`ry`(`post_code`, `psot_name`, `post_sort`, `status`) VALUES (?,?,?,?,?);");
            pstmt.setString(1,code);
            pstmt.setString(2,name);
            pstmt.setString(3,sort);
            pstmt.setString(4,status);
            pstmt.setString(5,time);
            add = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return add;
    }

    @Override
    public int delete(String id) {
        int delete = 0;
        super.init();
        conn = super.getConnection();
        try {
            pstmt = conn.prepareStatement("DELETE FROM ry WHERE post_id = ?");
            pstmt.setString(1,id);
            delete = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return delete;
    }

    @Override
    public Rs addone(String id) {
        super.init();
        conn = super.getConnection();
        try {
            pstmt = conn.prepareStatement("select * from ry where post_id=?");
            pstmt.setString(1,id);
            rs = pstmt.executeQuery();
            if (rs.next()){
                 rs1 = new Rs();
                 rs1.setPost_id(rs.getString("post_id"));
                 rs1.setPost_sort(rs.getString("post_sort"));
                 rs1.setPost_name(rs.getString("psot_name"));
                 rs1.setPost_code(rs.getString("post_code"));
                 rs1.setStatus(rs.getString("status"));
                 rs1.setTime(rs.getString("time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs1;
    }

    @Override
    public int update(Rs rs, String id) {
        int update = 0;
        super.init();
        conn = super.getConnection();
        try {
            pstmt = conn.prepareStatement("UPDATE ry SET post_code=?,psot_name = ?,post_sort=?,status=? time=? WHERE post_id=?");
            pstmt.setString(1,rs.getPost_code());
            pstmt.setString(2,rs.getPost_name());
            pstmt.setString(3,rs.getPost_sort());
            pstmt.setString(4,rs.getStatus());
            pstmt.setString(5,rs.getTime());
            pstmt.setString(6,id);
            update = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }
}
